﻿Ext.namespace('EHSubmit.UserInfo');


EHSubmit.UserInfo.Info = {
    xtype: 'form',
    id: 'InfoForm',
    style: 'margin:0 auto',
    title: '用户个人信息',
    labelAlign: 'left',
    reader: new Ext.data.JsonReader(
         { root: 'data' },
         [
             { name: 'Name', mapping: 'Name' },
             { name: 'Sex', mapping: 'Sex' },
             { name: 'BirthDay', mapping: 'Birthday'},
             { name: 'Email', mapping: 'Email' },
             { name: 'Phone', mapping: 'Phone' },
             { name: 'Institute', mapping: 'Institute' },
             { name: 'Department', mapping: 'Department' },
             { name: 'Class', mapping: 'Class' }
         ]),
    listeners: {
        render: function() {
                var frm = Ext.getCmp('InfoForm');
               
                frm.getForm().load({ url: EHSubmit.ajaxRoot + 'UserHandler.ashx?op=GetUser', method: 'GET', waitMsg: 'Loading' });
            }
    },
    buttonAlign: 'center',
    bodyStyle: 'padding:15px;',
    frame: true,
    border: false,
    labelWidth: 80,
    monitorValid: true,
    defaultType: 'textfield',
    width: 330,
    defaults: { anchor: '95%', msgTarget: 'side' },
    items: [{
        fieldLabel: '姓名',
        name: 'Name',
        boxMaxWidth: 120,
        allowBlank: false
    }, {
        xtype: 'combo',
        hiddenName: 'Sex',
        fieldLabel: '性别',
        forceSelection: true,
        store: ['男', '女'],
        triggerAction: "all",
        boxMaxWidth: 120,
        allowBlank: false
    }, {
        xtype: 'datefield',
        fieldLabel: '出生年月',
        format: 'Y-m-d',
        boxMaxWidth: 120,
        name: 'BirthDay'
    }, {
        fieldLabel: 'Email',
        name: 'Email',
        vtype: 'email'
    }, {
        //xtype: 'numberfield',
        fieldLabel: '联系电话',
        name: 'Phone'
    }, {
        fieldLabel: '学院',
        name: 'Institute',
        allowBlank: false
    }, {
        fieldLabel: '专业',
        name: 'Department',
        allowBlank: false
    } 
        ],
    buttons: [{
        text: '保存修改',
        handler: function() {
            var frm = Ext.getCmp('InfoForm');
            if (frm.getForm().isValid()) {  //验证
                frm.getForm().submit({  //提交
                    url: EHSubmit.ajaxRoot + 'UserHandler.ashx?op=Update',
                    waitTitle: '等待',
                    waitMsg: '正在提交，请稍后...',
                    method: 'POST',
                    success: function(form, action) {
                        var successed = action.result.success;
                        if (successed) {
                            ///////////////  提交成功处理  ///////////////////
                            Ext.MessageBox.alert('提示', '提交成功！');
                            var frm = Ext.getCmp('InfoForm');
                            frm.getForm().load({ url: EHSubmit.ajaxRoot + 'UserHandler.ashx?op=GetUser', method: 'GET', waitMsg: 'Loading' });
                        }
                        else {
                            Ext.MessageBox.alert('失败', '信息：' + action.result.msg);
                        }
                    },
                    failure: function(form, action) {
                        var errmsg = action.result.msg;
                        Ext.MessageBox.alert('错误', errmsg);
                    }   // failure  
                })
            }
        }

    }, {
        text: '刷新信息',
        handler: function() {
            var frm = Ext.getCmp('InfoForm');
            frm.getForm().load({ url: EHSubmit.ajaxRoot + 'UserHandler.ashx?op=GetUser', method: 'GET', waitMsg: 'Loading' });
        }

    }]
    };




/////验证重复输入密码是否一致
Ext.apply(Ext.form.VTypes, {
    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },

    passwordText: '输入的两次密码不一致'
});



EHSubmit.UserInfo.AlterPwd = {
    xtype: 'form',
    id: 'PwdForm',
    title: '修改密码',
    labelAlign: 'left',
    buttonAlign: 'center',
    bodyStyle: 'padding:15px;',
    frame: true,
    border: false,
    width: 350,
    style: 'margin:0 auto',
    defaultType: 'textfield',
    defaults: { width: 200, 
        inputType: 'password'},
    items: [{
        fieldLabel: '输入旧密码',
        name: 'OldPwd',
        id: 'OldPwd',
        allowBlank: false
    }, {
        fieldLabel: '输入新密码',
        name: 'NewPwd',
        id: 'NewPwd',
        allowBlank: false
    }, {
        fieldLabel: '确认新密码',
        name: 'Pwd-cfrm',
        vtype: 'password',
        allowBlank: false,
        initialPassField: 'NewPwd' // id of the initial password field
    }],
    buttons:[{
        text:'提交',
        handler: function() {
        var frm = Ext.getCmp('PwdForm');
            var a = frm.getForm();
            if (frm.getForm().isValid()) {  //验证
                frm.getForm().submit({  //提交
                    url: EHSubmit.ajaxRoot + 'UserHandler.ashx?op=AlterPwd',
                    waitTitle: '等待',
                    waitMsg: '正在提交，请稍后...',
                    method: 'POST',
                    success: function(form, action) {
                        var successed = action.result.success;
                        if (successed) {
                            ///////////////  提交成功处理  ///////////////////
                            Ext.MessageBox.alert('提示', '提交成功！');
                            Ext.getCmp('PwdForm').getForm().reset();
                        }
                        else {
                            Ext.MessageBox.alert('失败', '信息：' + action.result.msg);
                        }
                    },
                    failure: function(form, action) {
                        var errmsg = action.result.msg;
                        Ext.MessageBox.alert('错误', errmsg);
                    }   // failure  
               })
        }
     }
    }]
};

////////////////////////////////////////////////////////////////
EHSubmit.UserInfo.AlterPwdTab = {
    id: 'alterPwd',
    title: '修改密码',
    bodyStyle: 'padding:5px;',
    closable: true,
    items: [EHSubmit.UserInfo.AlterPwd]
};


EHSubmit.UserInfo.InfoTab = {
    id: 'UserInfo',
    title: '个人信息',
    bodyStyle: 'padding:5px;',
    closable: true,
    items: [EHSubmit.UserInfo.Info]
};